class ProfilesController < ApplicationController

  before_filter :login_required, :only => 'home'
  around_filter :limit_to_active_users

  def index
    @profiles = User.paginate :page => params[:page],
      :order => 'users.created_at desc',
      :include => [:photo, :current_job],
      :conditions => find_conditions(params)
  end

  def show
    @user = User.find_by_param(params[:id]) or raise ActiveRecord::RecordNotFound
    @articles = @user.articles(:limit => 10)
  end

  def home 
    @user = User.find_by_param(params[:id]) or raise ActiveRecord::RecordNotFound
    @articles = @user.articles(:limit => 10)
  end

  def articles
    @user = User.find_by_param(params[:id]) or raise ActiveRecord::RecordNotFound
    @articles = @user.articles.paginate :page => params[:page]
  end

  private

  def find_conditions(params)
    if params[:search]
      User.build_profile_lookup(params[:search])
    elsif params[:institution] or params[:position] or params[:department] or params[:city]
      User.build_advanced_search(params)
    end
  end

  def limit_to_active_users
    User.with_active do 
      yield
    end
  end

end
